package com.py.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.thrift.TException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.py.remote.Hello;
import com.py.remote.HelloRemote;

@RestController
public class ConsumerController {
    @Autowired
    HelloRemote HelloRemote;
	@Resource(name="helloService")
	private Hello.Iface helloService;
	
    @RequestMapping("/hello/{name}")
    public String index(@PathVariable("name") String name) {
    	
    	long total_mills = 0;
    	String mString = null;
    	List<String> msg = new ArrayList<>();
    	
    	for (int i = 0; i < 10000; i++) {
			long start_mills = System.currentTimeMillis();
			mString = HelloRemote.hello(name);
			if (!msg.contains(mString)) {
				msg.add(mString);
			}
			long end_mills = System.currentTimeMillis();
			long cost_mills = end_mills - start_mills;
			total_mills = total_mills + cost_mills;
		}
    	System.out.println("call cluod hello 10 num cost millis ["+ total_mills +"] ms");
    	
        return msg.toString();
    }
    @RequestMapping("/thrift/{name}")
    public String thrift(@PathVariable("name") String name) throws TException {
    	
    	long total_mills = 0;
    	String mString = null;
    	List<String> msg = new ArrayList<>();
    	
    	for (int i = 0; i < 10; i++) {
			long start_mills = System.currentTimeMillis();
			mString = helloService.helloString(name);
			if (!msg.contains(mString)) {
				msg.add(mString);
			}
			long end_mills = System.currentTimeMillis();
			long cost_mills = end_mills - start_mills;
			total_mills = total_mills + cost_mills;
		}
    	System.out.println("call thrift hello 10 num cost millis ["+ total_mills +"] ms");
    	
        return msg.toString();
    }
}
